﻿using Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WsdShop.IService;
using WsdShop.Model;
using WsdShop.Model.SearchModels;

namespace Admin.Controllers
{
    public class CouponController : BaseController
    {
        private ICouponService _couponService;
        private IProductCategoryService _pcService;
        public CouponController(IUserInfoService s,ICouponService couponService, IProductCategoryService pcService) : base(s) {
            _couponService = couponService;
            _pcService = pcService;
        }


        public ActionResult CounponList()
        {
            return View();
        }


        public ActionResult CouponInfo()
        {
            return View();
        }

        public ActionResult CouponDetails()
        {
            return View();
        }

        public JsonResult GetCouponInfo(int id)
        {
            Result result = new Result();
            try
            {
                result.data = _couponService.GetCouponInfo(id);
                result.isSuccess = true;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }

            return Json(result);
        }


        public JsonResult GetCounponList(BaseSearchModel search)
        {
            Result result = new Result();
            try
            {
                int total = 0;
                result.data = _couponService.GetCouponList(search, ref total);
                result.count = total;
                result.isSuccess = true;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }

            return Json(result);
        }

        public JsonResult GetCounponDetailList(MemberCouponSearch search)
        {
            Result result = new Result();
            try
            {
                int total = 0;
                result.data = _couponService.GetCouponDetailList(search, ref total);
                result.count = total;
                result.isSuccess = true;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }

            return Json(result);
        }

        public JsonResult DelCounpon(int id)
        {
            Result result = new Result();
            try
            {
                _couponService.DelCoupon(id);
                result.isSuccess = true;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }

            return Json(result);
        }

        public JsonResult SaveCoupon(Coupon model)
        {
            Result result = new Result();
            try
            {
                if (model.Amount <= 0)
                {
                    result.msg = "优惠券金额必须大于0";
                    return Json(result);
                }
                if (model.Consumption < 0)
                {
                    result.msg = "优惠券最低消费金额不能为负数";
                    return Json(result);
                }
                if (model.Id > 0)
                {
                    Coupon coupon = _couponService.FindCoupon(model.Id);
                    if (coupon != null)
                    {
                        coupon.Category = model.Category;
                        coupon.CouponName = model.CouponName;
                        coupon.Amount = model.Amount;
                        coupon.Consumption = model.Consumption;
                        coupon.Describe = model.Describe;
                        coupon.EditTime = DateTime.Now;
                        coupon.EditUserId = CurrentLoginUser.Id;
                        coupon.EndTime = model.EndTime;
                        coupon.IsOpenReceive = model.IsOpenReceive;
                        coupon.IsUseActivity = model.IsUseActivity;
                        coupon.IsUsePoint = model.IsUsePoint;
                        coupon.StartTime = model.StartTime;
                        coupon.TotalNum = model.TotalNum;

                        _couponService.EditCoupon(coupon);
                    }
                }
                else
                {
                    model.AddUserId = CurrentLoginUser.Id;
                    _couponService.AddCoupon(model);
                }
                result.isSuccess = true;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }

            return Json(result);
        }

        public JsonResult GetCategoryList()
        {
            Result result = new Result();
            try
            {
                var list = _pcService.GetProductCategoryList(0);
                Product_Category pc = new Product_Category();
                pc.Id = 0;
                pc.CategoryName = "全品类";
                list.Insert(0, pc);
                result.data = list;
                result.isSuccess = true;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }

            return Json(result);
        }
    }
}